// pages/courseDetail/index.js
const app = getApp();

Page({
  makeCall(event) {
    console.log(event.currentTarget);
    if (event.currentTarget.dataset.phonenu != null) {
      wx.makePhoneCall({
        phoneNumber: event.currentTarget.dataset.phonenu,
        success: function() {
          console.log("拨打电话成功！");
        },
        fail: function() {
          console.log("拨打电话失败！");
        }
      });
    }
  },

  /**
   * 页面的初始数据
   */
  data: {
    course: null,
    files: [],
    files2: [],
    user: null,
    _reserve: null
  },

  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    });
  },

  previewImage2: function(e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files2 // 需要预览的图片http链接列表
    });
  },

  getTeacherDetail(id) {
    let that = this;

    wx.request({
      url: app.globalData.serverUrl + "/user/queryById",
      header: {
        token: app.globalData.token
      },
      data: {
        userId: id
      },
      success(resp) {
        console.log(resp);
        if (resp.data.success) {
          that.setData({
            user: resp.data.data,
            files2: resp.data.data.photographList
          });
        }
      },
      fail(resp) {
        console.error(resp);
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    let that = this;

    let reserve = JSON.parse(options.reserve);

    wx.request({
      url: app.globalData.serverUrl + "/course/queryById",
      header: {
        token: app.globalData.token
      },
      data: {
        courseId: reserve.courseId
      },
      success(resp) {
        console.log(resp);
        if (resp.data.success) {
          that.setData({
            course: resp.data.data,
            files: resp.data.data.photographList,
            _reserve: reserve
          });

          that.getTeacherDetail(reserve.teacherId);
        }
      },
      fail(resp) {
        console.error(resp);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
